package com.zmn.brs.admin.controller.scene;

import com.zmn.brs.model.entity.event.Event;
import com.zmn.brs.model.entity.event.EventQuery;
import com.zmn.brs.model.entity.scene.Scene;
import com.zmn.brs.model.entity.scene.SceneQuery;
import com.zmn.brs.services.interfaces.event.EventService;
import com.zmn.brs.services.interfaces.scene.SceneService;
import com.zmn.common.utils.string.StringUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述: 业务场景配置
 *
 * @author chongw
 * @since 2021-05-27
 **/
@Controller
@RequestMapping(value = "/brs/scene/")
public class SceneListController extends SceneBaseController {

    @Resource
    private SceneService sceneService;

    @Resource
    private EventService eventService;

    /**
     * 描述: 业务场景列表
     * @param query 查询条件
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping(value = "list")
    @RequiresPermissions("/brs/scene/list.action")
    public ModelAndView list(@ModelAttribute SceneQuery query) {
        if (StringUtil.isNotBlank(query.getTimeSearch())) {
            String[] timeRangeArray = query.getTimeSearch().split("-");
            query.setStartUpdateTime(timeRangeArray[0].trim() + " 00:00:00");
            query.setEndUpdateTime(timeRangeArray[1].trim() + " 23:59:59");
        }
        List<Scene> list = sceneService.listPageByQuery(query);
        ModelAndView mv = new ModelAndView("scene/list");
        mv.addObject("items",list);
        mv.addObject("vo", query);
        return mv;
    }

    /**
     * 描述: 查询业务场景信息
     * @param query 查询条件
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping(value = "info")
    public ModelAndView info(@ModelAttribute SceneQuery query) {
        Scene data = sceneService.findById(query.getSceneId());
        ModelAndView mv = new ModelAndView("scene/info");
        mv.addObject("data",data);
        mv.addObject("sceneId",query.getSceneId());
        mv.addObject("flag",1);
        return mv;
    }

    /**
     * 描述: 业务场景事件列表
     * @param query 查询条件
     * @return org.springframework.web.servlet.ModelAndView
     **/
    @RequestMapping(value = "eventList")
    public ModelAndView list(@ModelAttribute EventQuery query) {
        //查询场景信息
        Scene scene = sceneService.findById(query.getSceneId());
        if (StringUtil.isNotBlank(query.getTimeSearch())) {
            String[] timeRangeArray = query.getTimeSearch().split("-");
            query.setStartUpdateTime(timeRangeArray[0].trim() + " 00:00:00");
            query.setEndUpdateTime(timeRangeArray[1].trim() + " 23:59:59");
        }
        List<Event> list = eventService.listPageByQuery(query);
        ModelAndView mv = new ModelAndView("scene/event");
        mv.addObject("items",list);
        mv.addObject("vo", query);
        mv.addObject("sceneId",query.getSceneId());
        mv.addObject("scene",scene);
        mv.addObject("flag",2);
        return mv;
    }

}